package edu.mse.camel.client.whiteboard.diagram.navigator;

import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.gmf.runtime.common.ui.services.parser.IParser;
import org.eclipse.gmf.runtime.common.ui.services.parser.ParserOptions;
import org.eclipse.gmf.runtime.common.ui.services.parser.ParserService;
import org.eclipse.gmf.runtime.emf.type.core.IElementType;
import org.eclipse.gmf.runtime.notation.View;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.resource.ImageRegistry;
import org.eclipse.jface.viewers.ITreePathLabelProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.TreePath;
import org.eclipse.jface.viewers.ViewerLabel;
import org.eclipse.swt.graphics.Image;
import org.eclipse.ui.IMemento;
import org.eclipse.ui.navigator.ICommonContentExtensionSite;
import org.eclipse.ui.navigator.ICommonLabelProvider;

import edu.mse.camel.client.whiteboard.diagram.edit.parts.CylinderEditPart;
import edu.mse.camel.client.whiteboard.diagram.edit.parts.CylinderNameEditPart;
import edu.mse.camel.client.whiteboard.diagram.edit.parts.DashArrowConnEditPart;
import edu.mse.camel.client.whiteboard.diagram.edit.parts.DashConnEditPart;
import edu.mse.camel.client.whiteboard.diagram.edit.parts.DashDoubleArrowConnEditPart;
import edu.mse.camel.client.whiteboard.diagram.edit.parts.DiagramEditPart;
import edu.mse.camel.client.whiteboard.diagram.edit.parts.DiamondEditPart;
import edu.mse.camel.client.whiteboard.diagram.edit.parts.DiamondNameEditPart;
import edu.mse.camel.client.whiteboard.diagram.edit.parts.EllipseEditPart;
import edu.mse.camel.client.whiteboard.diagram.edit.parts.EllipseNameEditPart;
import edu.mse.camel.client.whiteboard.diagram.edit.parts.RectangleEditPart;
import edu.mse.camel.client.whiteboard.diagram.edit.parts.RectangleNameEditPart;
import edu.mse.camel.client.whiteboard.diagram.edit.parts.RoundedRectangleEditPart;
import edu.mse.camel.client.whiteboard.diagram.edit.parts.RoundedRectangleNameEditPart;
import edu.mse.camel.client.whiteboard.diagram.edit.parts.SolidArrowConnEditPart;
import edu.mse.camel.client.whiteboard.diagram.edit.parts.SolidConnEditPart;
import edu.mse.camel.client.whiteboard.diagram.edit.parts.SolidDoubleArrowConnEditPart;
import edu.mse.camel.client.whiteboard.diagram.part.WhiteboardDiagramEditorPlugin;
import edu.mse.camel.client.whiteboard.diagram.part.WhiteboardVisualIDRegistry;
import edu.mse.camel.client.whiteboard.diagram.providers.WhiteboardElementTypes;
import edu.mse.camel.client.whiteboard.diagram.providers.WhiteboardParserProvider;

/**
 * @generated
 */
public class WhiteboardNavigatorLabelProvider extends LabelProvider implements
		ICommonLabelProvider, ITreePathLabelProvider {

	/**
	 * @generated
	 */
	static {
		WhiteboardDiagramEditorPlugin
				.getInstance()
				.getImageRegistry()
				.put(
						"Navigator?UnknownElement", ImageDescriptor.getMissingImageDescriptor()); //$NON-NLS-1$
		WhiteboardDiagramEditorPlugin
				.getInstance()
				.getImageRegistry()
				.put(
						"Navigator?ImageNotFound", ImageDescriptor.getMissingImageDescriptor()); //$NON-NLS-1$
	}

	/**
	 * @generated
	 */
	public void updateLabel(ViewerLabel label, TreePath elementPath) {
		Object element = elementPath.getLastSegment();
		if (element instanceof WhiteboardNavigatorItem
				&& !isOwnView(((WhiteboardNavigatorItem) element).getView())) {
			return;
		}
		label.setText(getText(element));
		label.setImage(getImage(element));
	}

	/**
	 * @generated
	 */
	public Image getImage(Object element) {
		if (element instanceof WhiteboardNavigatorGroup) {
			WhiteboardNavigatorGroup group = (WhiteboardNavigatorGroup) element;
			return WhiteboardDiagramEditorPlugin.getInstance().getBundledImage(
					group.getIcon());
		}

		if (element instanceof WhiteboardNavigatorItem) {
			WhiteboardNavigatorItem navigatorItem = (WhiteboardNavigatorItem) element;
			if (!isOwnView(navigatorItem.getView())) {
				return super.getImage(element);
			}
			return getImage(navigatorItem.getView());
		}

		return super.getImage(element);
	}

	/**
	 * @generated
	 */
	public Image getImage(View view) {
		switch (WhiteboardVisualIDRegistry.getVisualID(view)) {
		case DiagramEditPart.VISUAL_ID:
			return getImage(
					"Navigator?Diagram?http://edu.mse.camel.client.whiteboard/whiteboard.encore?Diagram", WhiteboardElementTypes.Diagram_79); //$NON-NLS-1$
		case CylinderEditPart.VISUAL_ID:
			return getImage(
					"Navigator?TopLevelNode?http://edu.mse.camel.client.whiteboard/whiteboard.encore?Cylinder", WhiteboardElementTypes.Cylinder_1001); //$NON-NLS-1$
		case EllipseEditPart.VISUAL_ID:
			return getImage(
					"Navigator?TopLevelNode?http://edu.mse.camel.client.whiteboard/whiteboard.encore?Ellipse", WhiteboardElementTypes.Ellipse_1002); //$NON-NLS-1$
		case RectangleEditPart.VISUAL_ID:
			return getImage(
					"Navigator?TopLevelNode?http://edu.mse.camel.client.whiteboard/whiteboard.encore?Rectangle", WhiteboardElementTypes.Rectangle_1003); //$NON-NLS-1$
		case DiamondEditPart.VISUAL_ID:
			return getImage(
					"Navigator?TopLevelNode?http://edu.mse.camel.client.whiteboard/whiteboard.encore?Diamond", WhiteboardElementTypes.Diamond_1004); //$NON-NLS-1$
		case RoundedRectangleEditPart.VISUAL_ID:
			return getImage(
					"Navigator?TopLevelNode?http://edu.mse.camel.client.whiteboard/whiteboard.encore?RoundedRectangle", WhiteboardElementTypes.RoundedRectangle_1005); //$NON-NLS-1$
		case SolidConnEditPart.VISUAL_ID:
			return getImage(
					"Navigator?Link?http://edu.mse.camel.client.whiteboard/whiteboard.encore?SolidConn", WhiteboardElementTypes.SolidConn_3001); //$NON-NLS-1$
		case SolidArrowConnEditPart.VISUAL_ID:
			return getImage(
					"Navigator?Link?http://edu.mse.camel.client.whiteboard/whiteboard.encore?SolidArrowConn", WhiteboardElementTypes.SolidArrowConn_3002); //$NON-NLS-1$
		case SolidDoubleArrowConnEditPart.VISUAL_ID:
			return getImage(
					"Navigator?Link?http://edu.mse.camel.client.whiteboard/whiteboard.encore?SolidDoubleArrowConn", WhiteboardElementTypes.SolidDoubleArrowConn_3003); //$NON-NLS-1$
		case DashConnEditPart.VISUAL_ID:
			return getImage(
					"Navigator?Link?http://edu.mse.camel.client.whiteboard/whiteboard.encore?DashConn", WhiteboardElementTypes.DashConn_3004); //$NON-NLS-1$
		case DashArrowConnEditPart.VISUAL_ID:
			return getImage(
					"Navigator?Link?http://edu.mse.camel.client.whiteboard/whiteboard.encore?DashArrowConn", WhiteboardElementTypes.DashArrowConn_3005); //$NON-NLS-1$
		case DashDoubleArrowConnEditPart.VISUAL_ID:
			return getImage(
					"Navigator?Link?http://edu.mse.camel.client.whiteboard/whiteboard.encore?DashDoubleArrowConn", WhiteboardElementTypes.DashDoubleArrowConn_3006); //$NON-NLS-1$
		}
		return getImage("Navigator?UnknownElement", null); //$NON-NLS-1$
	}

	/**
	 * @generated
	 */
	private Image getImage(String key, IElementType elementType) {
		ImageRegistry imageRegistry = WhiteboardDiagramEditorPlugin
				.getInstance().getImageRegistry();
		Image image = imageRegistry.get(key);
		if (image == null && elementType != null
				&& WhiteboardElementTypes.isKnownElementType(elementType)) {
			image = WhiteboardElementTypes.getImage(elementType);
			imageRegistry.put(key, image);
		}

		if (image == null) {
			image = imageRegistry.get("Navigator?ImageNotFound"); //$NON-NLS-1$
			imageRegistry.put(key, image);
		}
		return image;
	}

	/**
	 * @generated
	 */
	public String getText(Object element) {
		if (element instanceof WhiteboardNavigatorGroup) {
			WhiteboardNavigatorGroup group = (WhiteboardNavigatorGroup) element;
			return group.getGroupName();
		}

		if (element instanceof WhiteboardNavigatorItem) {
			WhiteboardNavigatorItem navigatorItem = (WhiteboardNavigatorItem) element;
			if (!isOwnView(navigatorItem.getView())) {
				return null;
			}
			return getText(navigatorItem.getView());
		}

		return super.getText(element);
	}

	/**
	 * @generated
	 */
	public String getText(View view) {
		if (view.getElement() != null && view.getElement().eIsProxy()) {
			return getUnresolvedDomainElementProxyText(view);
		}
		switch (WhiteboardVisualIDRegistry.getVisualID(view)) {
		case DiagramEditPart.VISUAL_ID:
			return getDiagram_79Text(view);
		case CylinderEditPart.VISUAL_ID:
			return getCylinder_1001Text(view);
		case EllipseEditPart.VISUAL_ID:
			return getEllipse_1002Text(view);
		case RectangleEditPart.VISUAL_ID:
			return getRectangle_1003Text(view);
		case DiamondEditPart.VISUAL_ID:
			return getDiamond_1004Text(view);
		case RoundedRectangleEditPart.VISUAL_ID:
			return getRoundedRectangle_1005Text(view);
		case SolidConnEditPart.VISUAL_ID:
			return getSolidConn_3001Text(view);
		case SolidArrowConnEditPart.VISUAL_ID:
			return getSolidArrowConn_3002Text(view);
		case SolidDoubleArrowConnEditPart.VISUAL_ID:
			return getSolidDoubleArrowConn_3003Text(view);
		case DashConnEditPart.VISUAL_ID:
			return getDashConn_3004Text(view);
		case DashArrowConnEditPart.VISUAL_ID:
			return getDashArrowConn_3005Text(view);
		case DashDoubleArrowConnEditPart.VISUAL_ID:
			return getDashDoubleArrowConn_3006Text(view);
		}
		return getUnknownElementText(view);
	}

	/**
	 * @generated
	 */
	private String getDiagram_79Text(View view) {
		return ""; //$NON-NLS-1$
	}

	/**
	 * @generated
	 */
	private String getCylinder_1001Text(View view) {
		IAdaptable hintAdapter = new WhiteboardParserProvider.HintAdapter(
				WhiteboardElementTypes.Cylinder_1001,
				(view.getElement() != null ? view.getElement() : view),
				WhiteboardVisualIDRegistry
						.getType(CylinderNameEditPart.VISUAL_ID));
		IParser parser = ParserService.getInstance().getParser(hintAdapter);

		if (parser != null) {
			return parser.getPrintString(hintAdapter, ParserOptions.NONE
					.intValue());
		} else {
			WhiteboardDiagramEditorPlugin.getInstance().logError(
					"Parser was not found for label " + 4001); //$NON-NLS-1$
			return ""; //$NON-NLS-1$
		}

	}

	/**
	 * @generated
	 */
	private String getEllipse_1002Text(View view) {
		IAdaptable hintAdapter = new WhiteboardParserProvider.HintAdapter(
				WhiteboardElementTypes.Ellipse_1002,
				(view.getElement() != null ? view.getElement() : view),
				WhiteboardVisualIDRegistry
						.getType(EllipseNameEditPart.VISUAL_ID));
		IParser parser = ParserService.getInstance().getParser(hintAdapter);

		if (parser != null) {
			return parser.getPrintString(hintAdapter, ParserOptions.NONE
					.intValue());
		} else {
			WhiteboardDiagramEditorPlugin.getInstance().logError(
					"Parser was not found for label " + 4002); //$NON-NLS-1$
			return ""; //$NON-NLS-1$
		}

	}

	/**
	 * @generated
	 */
	private String getRectangle_1003Text(View view) {
		IAdaptable hintAdapter = new WhiteboardParserProvider.HintAdapter(
				WhiteboardElementTypes.Rectangle_1003,
				(view.getElement() != null ? view.getElement() : view),
				WhiteboardVisualIDRegistry
						.getType(RectangleNameEditPart.VISUAL_ID));
		IParser parser = ParserService.getInstance().getParser(hintAdapter);

		if (parser != null) {
			return parser.getPrintString(hintAdapter, ParserOptions.NONE
					.intValue());
		} else {
			WhiteboardDiagramEditorPlugin.getInstance().logError(
					"Parser was not found for label " + 4003); //$NON-NLS-1$
			return ""; //$NON-NLS-1$
		}

	}

	/**
	 * @generated
	 */
	private String getDiamond_1004Text(View view) {
		IAdaptable hintAdapter = new WhiteboardParserProvider.HintAdapter(
				WhiteboardElementTypes.Diamond_1004,
				(view.getElement() != null ? view.getElement() : view),
				WhiteboardVisualIDRegistry
						.getType(DiamondNameEditPart.VISUAL_ID));
		IParser parser = ParserService.getInstance().getParser(hintAdapter);

		if (parser != null) {
			return parser.getPrintString(hintAdapter, ParserOptions.NONE
					.intValue());
		} else {
			WhiteboardDiagramEditorPlugin.getInstance().logError(
					"Parser was not found for label " + 4004); //$NON-NLS-1$
			return ""; //$NON-NLS-1$
		}

	}

	/**
	 * @generated
	 */
	private String getRoundedRectangle_1005Text(View view) {
		IAdaptable hintAdapter = new WhiteboardParserProvider.HintAdapter(
				WhiteboardElementTypes.RoundedRectangle_1005, (view
						.getElement() != null ? view.getElement() : view),
				WhiteboardVisualIDRegistry
						.getType(RoundedRectangleNameEditPart.VISUAL_ID));
		IParser parser = ParserService.getInstance().getParser(hintAdapter);

		if (parser != null) {
			return parser.getPrintString(hintAdapter, ParserOptions.NONE
					.intValue());
		} else {
			WhiteboardDiagramEditorPlugin.getInstance().logError(
					"Parser was not found for label " + 4005); //$NON-NLS-1$
			return ""; //$NON-NLS-1$
		}

	}

	/**
	 * @generated
	 */
	private String getSolidConn_3001Text(View view) {
		return ""; //$NON-NLS-1$
	}

	/**
	 * @generated
	 */
	private String getSolidArrowConn_3002Text(View view) {
		return ""; //$NON-NLS-1$
	}

	/**
	 * @generated
	 */
	private String getSolidDoubleArrowConn_3003Text(View view) {
		return ""; //$NON-NLS-1$
	}

	/**
	 * @generated
	 */
	private String getDashConn_3004Text(View view) {
		return ""; //$NON-NLS-1$
	}

	/**
	 * @generated
	 */
	private String getDashArrowConn_3005Text(View view) {
		return ""; //$NON-NLS-1$
	}

	/**
	 * @generated
	 */
	private String getDashDoubleArrowConn_3006Text(View view) {
		return ""; //$NON-NLS-1$
	}

	/**
	 * @generated
	 */
	private String getUnknownElementText(View view) {
		return "<UnknownElement Visual_ID = " + view.getType() + ">"; //$NON-NLS-1$ //$NON-NLS-2$
	}

	/**
	 * @generated
	 */
	private String getUnresolvedDomainElementProxyText(View view) {
		return "<Unresolved domain element Visual_ID = " + view.getType() + ">"; //$NON-NLS-1$ //$NON-NLS-2$
	}

	/**
	 * @generated
	 */
	public void init(ICommonContentExtensionSite aConfig) {
	}

	/**
	 * @generated
	 */
	public void restoreState(IMemento aMemento) {
	}

	/**
	 * @generated
	 */
	public void saveState(IMemento aMemento) {
	}

	/**
	 * @generated
	 */
	public String getDescription(Object anElement) {
		return null;
	}

	/**
	 * @generated
	 */
	private boolean isOwnView(View view) {
		return DiagramEditPart.MODEL_ID.equals(WhiteboardVisualIDRegistry
				.getModelID(view));
	}

}
